from __future__ import absolute_import
from __future__ import division
import sys
import os
import argparse
import logging
from src import const
from src.segmentation import SegRegion
import numpy as np
import math


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--lat-n', type=int, default=20)
    parser.add_argument('-m', '--lng-n', type=int, default=20)
    parser.add_argument('-r', '--region', type=str, default='shanghai_test')
    parser.add_argument('--feature-file', default='data/shanghai_wgs.txt')
    parser.add_argument('--save-file', default='data/pic_y_feature.txt')
    return parser.parse_args()


def main(args):
    region = getattr(const, args.region.upper() + '_REGION')
    seg_region = SegRegion(region.left_up, region.right_down, args.lat_n, args.lng_n)
    seg_region.restaurant_y_feature(args.feature_file)
    with open(args.save_file, 'w') as save_file:
        print(seg_region.map[3][0].y)
        for i in range(args.lat_n):
            for j in range(args.lng_n):
                one_region = seg_region.map[i][j]
                save_file.write('%f %f %f %f ' % (one_region.left_up.lat, one_region.left_up.lng,
                                                  one_region.right_down.lat, one_region.right_down.lng))
                # if one_region.y != 0:
                #     print(one_region.y)
                save_file.write(str(math.log(one_region.y + 1)))
                save_file.write('\n')
        # for line_regions in seg_region.map:
        #     for one_region in line_regions:
        #         save_file.write('%f %f %f %f ' % (one_region.left_up.lat, one_region.left_up.lng,
        #                                           one_region.right_down.lat, one_region.right_down.lng))
        #         if region.y != 0:
        #             print(region.y)
        #         save_file.write(str(region.y))
        #         save_file.write('\n')
    return args.save_file

if __name__ == '__main__':
    args = parse_args()
    logging.basicConfig(level=logging.INFO)
    print(main(args))
